package util.content;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.http.client.params.CookiePolicy;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class SpiderContentTxt {
	// 抓取失败后多次重启，每次重启间隔的增量
	final int errorSleepIncrement = 30000;
	HtmlPage spiderPage = null;
	WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
	String title = null;
	String bodyString = null;

	public String getWebSiteBodyString() {
		String bodyString = this.bodyString;
		//System.out.println(bodyString);
		return bodyString;
	}

	public String getTitle() {

		String title = this.title;
		// System.out.println(title);
		return title;
	}

	public void clear() {
		webClient.closeAllWindows();
		webClient.getCookieManager().clearCookies();
	}

	public void SpiderPageContent(String site) {
		// webClient.getCookieManager().setCookiesEnabled(false);
		System.setProperty("apache.commons.httpclient.cookiespec", CookiePolicy.BROWSER_COMPATIBILITY);
		// webClient关闭javascript、设置最长连接时间15秒
		webClient.setJavaScriptEnabled(false);
		webClient.setCssEnabled(false);
		webClient.setTimeout(15000);
		// 重连接几次，如果
		for (int reConectTimes = 0; reConectTimes < repeatTimesContent;) {

			try {
				this.spiderPage = webClient.getPage(site);
			} catch (FailingHttpStatusCodeException e) {			
				if (e.getStatusCode() == 404) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else if (e.getStatusCode() == 400) {
					this.title = "0";
					this.bodyString = "0";
					break;
				}else if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			} catch (MalformedURLException e) {			
				if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			} catch (IOException e) {				
				if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			} catch (Exception e) {
				// 出错链连接错误，连续几次，如果连续连接最多次数title写成0000、content写成0000，否则暂停10秒继续连接
				if (reConectTimes == repeatTimesContent) {
					this.title = "0";
					this.bodyString = "0";
					break;
				} else
					try {
						Thread.sleep(errorSleepIncrement);
						reConectTimes++;
						continue;
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
			}
			title = this.spiderPage.getTitleText();
			this.bodyString = spiderPage.getBody().asXml();
			reConectTimes = reConectTimes + repeatTimesContent;

			/*
			 * catch (Exception e) { //
			 * 出错链连接错误，连续几次，如果连续连接最多次数title写成0000、content写成0000，否则暂停10秒继续连接 if
			 * (reConectTimes == repeatTimesContent) { this.title = "0";
			 * this.bodyString = "0"; break; } else try {
			 * Thread.sleep(intervalContent +
			 * errorSleepIncrement*reConectTimes); reConectTimes++; continue; }
			 * catch (InterruptedException e1) { e1.printStackTrace(); } }
			 */

		}
	}

	int repeatTimesContent = 0;

	public void setRepeatTimesContent(int repeatTimesContent) {
		this.repeatTimesContent = repeatTimesContent;
	}
}
